package cn.itsource.rpms.web.controller;

import cn.itsource.rpms.domain.RepairOrderItem;
import cn.itsource.rpms.service.IRepairOrderItemService;
import cn.itsource.rpms.service.IRepairOrderService;
import cn.itsource.rpms.util.AjaxResult;
import cn.itsource.rpms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/repairorderitem")
@CrossOrigin
public class RepairOrderItemController {
    @Autowired
    private IRepairOrderItemService service;
    @Autowired
    private IRepairOrderService repairOrderService;
    @RequestMapping(value = "/queryAll",method = RequestMethod.PATCH)
    @ResponseBody
    public List<RepairOrderItem> queryAll(){
        return service.queryAll();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public AjaxResult delete(@PathVariable Long id){
        try {
            service.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败");
        }
        return new AjaxResult();
    }
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public RepairOrderItem queryOne(@PathVariable Long id){//将地址栏的id作为参数来接收
        System.out.println(id);
        return service.queryOne(id);
    }
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult update(@RequestBody RepairOrderItem repairOrderItem){
        try {
            service.update(repairOrderItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("修改失败");
        }
        return new AjaxResult();
    }
    @RequestMapping(value = "/add",method = RequestMethod.PUT)
    @ResponseBody
    public AjaxResult add(@RequestBody RepairOrderItem repairOrderItem){//将接收过来的json数据转换成对应的实体类对象
        try {
            service.add(repairOrderItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("保存失败");//保存失败就返回不保存失败的ajaxresult
        }
        return new AjaxResult();//成功就使用默认的
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.POST)
    @ResponseBody
    public PageResult<RepairOrderItem> queryItem(@PathVariable Long id){
        List<RepairOrderItem> items = service.queryItem(id);
        PageResult<RepairOrderItem> pageResult = new PageResult<>();
        pageResult.setRows(items);
        return pageResult;

    }
}
